<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>商品管理</el-breadcrumb-item>
            <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card >
       <el-row class="iselrow">
            <el-button type="success" @click="addCate">添加分类</el-button>
       </el-row>
       <tree-table :data="catelist" :columns="columns" :selection-type="false" 
       show-index index-text="#" :expand-type="false" border>
            <template slot="isok" slot-scope="scope">
                <i class="el-icon-success" v-if="scope.row.cat_deleted==false" style="color:lightgreen"></i>
                <i class="el-icon-error" v-else style="color:red"></i>
            </template>
            <template slot="order" slot-scope="scope">
                <el-tag size="mini" v-if="scope.row.cat_level==0">一级</el-tag>
                <el-tag size="mini" v-if="scope.row.cat_level==1" type="success">二级</el-tag>
                <el-tag size="mini" v-if="scope.row.cat_level==2" type="warning">三级</el-tag>
            </template>
            <template slot="option" slot-scope="scope">
                <el-button type="primary" icon="el-icon-edit" size="mini" @click="editcate(scope.row)">编辑</el-button>
                <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleterole(scope.row)">删除</el-button> 
            </template>
       </tree-table>
         <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="querinfo.pagenum"
            :page-sizes="[5, 6, 7, 8]"
            :page-size="querinfo.pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
    </el-pagination>
    </el-card>
    <el-dialog
        title="添加分类"
        :visible.sync="addCatedialogVisible"
        width="50%" @close="addcatedialogclose">
        <el-form :model="addCateFrom" :rules="addCaterules" ref="addcateruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="分类名称 ：" prop="cat_name">
                <el-input v-model="addCateFrom.cat_name"></el-input>
            </el-form-item>
            <el-form-item label="父级分类 ：" >
                 <el-cascader
                    size='mini'
                    expand-trigger="hover"
                    v-model="selectkeys"
                    :options="parentCatelist"
                    :props="cascaerProps"
                    @change="parentcateChange">
                </el-cascader>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="addCatedialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="addCatesure">确 定</el-button>
        </span>
    </el-dialog>

    <!---编辑分类--->
    <el-dialog
        title="编辑分类"
        :visible.sync="eaitcatedialogVisible"
        width="50%">
        <el-form :model="addCateFrom" :rules="caterules" ref="cateruleForm" label-width="100px">
            <el-form-item label="分类名称" prop="cat_name">
                <el-input v-model="addCateFrom.cat_name"></el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="eaitcatedialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="editcatesure">确 定</el-button>
        </span>
    </el-dialog>

  </div>
</template>

<script>
export default {
    data() {
        return {
            querinfo:{
                type:3,
                pagenum:1,
                pagesize:5
            },
            addCatedialogVisible:false,
            eaitcatedialogVisible:false,
            addCateFrom:{
                cat_name:'',
                cat_pid:0,
                cat_level:0
            },
            parentCatelist:[],
            catelist:[],
            cascaerProps:{
                value:'cat_id',
                label:'cat_name',
                children:'children'
            },
            caterules:{
                cat_name:[{ required: true, message: '请输入分类名称', trigger: 'blur' },
                          { min: 2, max: 5, message: '长度在 2 到 6 个字符', trigger: 'blur' }]
            },
            selectkeys:[],
            cateid:0,
            total:0,
            columns:[{
                label:"分类名称",
                prop:"cat_name"
            },{
                label:'是否有效',
                type:'template',
                template:'isok'
            },{
                label:"排序",
                type:"template",
                template:'order'
            },{
                label:"操作",
                type:"template",
                template:'option'
            }],
            addCaterules:{
                cat_name:[{ required: true, message: '请输入分类名称', trigger: 'blur' },
                          { min: 2, max: 5, message: '长度在 2 到 6 个字符', trigger: 'blur' }]
            },
           
        }
    },
    created() {
        this.getCatelist()
    },
    methods: {
        async getCatelist(){
        const {data:res} =  await this.$http.get('categories',{
            params:this.querinfo
        })
        if(res.meta.status!=200) return this.$message.error("获取商品分类失败");
        this.catelist = res.data.result
        this.total =res.data.total
        },
        handleSizeChange(newSize){
            this.querinfo.pagesize =newSize
            this.getCatelist()
        },
        handleCurrentChange(newpagenum){
            this.querinfo.pagenum = newpagenum
            this.getCatelist()
        },
        addCate(){
            this.getparentCatelist()
            this.addCatedialogVisible =true
            
        },
        async getparentCatelist(){
            const {data:res} = await this.$http.get("categories",{
                params:{
                    type:2
                }
            })
            if(res.meta.status!=200) return tis.$message.error("获取父级分类数据失败")
            this.parentCatelist = res.data
            
        },
        parentcateChange(){
           if(this.selectkeys.length>0){
               this.addCateFrom.cat_pid=this.selectkeys[this.selectkeys.length-1]
               this.addCateFrom.cat_level=this.selectkeys.length
               return 

           }
           else{
               this.addCateFrom.cat_pid=0
               this.addCateFrom.cat_level=0
               return 
           }
            
        },
        addCatesure(){
            this.$refs.addcateruleForm.validate(async vaild=>{
                if(!vaild) return 
                const {data:res} = await this.$http.post('categories',this.addCateFrom)
               if(res.meta.status!=201) return this.$message.error("添加分类商品失败")
               this.$message.success("添加分类商品成功")
               this.addCatedialogVisible =false
               this.getCatelist() 
            })
            
        },
        addcatedialogclose(){
            this.$refs.addcateruleForm.resetFields()
            this.selectkeys=[],
            this.addCateFrom.cat_level=0,
            this.addCateFrom.cat_pid = 0
        },
        async deleterole(role){
            console.log(role); 
            const res = await this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'error'
            }).catch(err=>err)
            if(res!='confirm'){
            return this.$message.info("取消了删除")
            } 
            const {data:result} = await this.$http.delete(`categories/${role.cat_id}`) 
            if(result.meta.status!=200){
                return this.$message.error("删除失败")
            }
            this.getCatelist()
            this.$message.success("删除成功")
            
        },
        editcate(row){
            this.cateid =row.cat_id
            this.eaitcatedialogVisible=true
        },
       async editcatesure(){
            const {data:res} =  await this.$http.put(`categories/${this.cateid}`,{
                      cat_name:this.addCateFrom.cat_name  
              })
              if(res.meta.status!=200) return this.$message.error("更新分类名称失败")
              this.eaitcatedialogVisible = false
              this.getCatelist()  
              this.$message.success("更新分类名称成功")
               
        }
    },
}
</script>

<style scoped>
.iselrow{
    margin-bottom: 10px;
}
</style>